/**
 * Created by wry on 2015/4/13.
 */
var activityService = require('../../services/backoffice/backOffice-activityService');
var messageUtil = require('../../common/utils/messageUtil');
var toolUtil = require('../../common/utils/toolUtil');

exports.getBackOfficeActivityList = function (req, res) {
    var communityId = req.params.communityId;
    var page = req.params.page;
    var level = req.params.level;

    activityService.getBackOfficeActivityListService(communityId, page,level, function (err, result) {
        if (err) {
            logger.error('Exception happened when getBackOfficeActivityList...!' + err.message);
            res.json(messageUtil.composeFailedMessage(req, 'Exception happened when getBackOfficeActivityList...!' + err.message));
        } else {
            var data = {
                aList: result[0],
                totalPage: result[1]
            };
            res.json(messageUtil.composeSucceededMessage(req, null, data));
        }
    });
};

exports.moveActivityToTop = function (req, res) {
    var input = req.body;

    activityService.moveActivityToTopService(input, function (err, result) {
        if (err) {
            logger.error('Exception happened when moveActivityToTopService...!' + err.message);
            res.json(messageUtil.composeFailedMessage(req, 'Exception happened when moveActivityToTopService...!' + err.message));
        } else {
            res.json(messageUtil.composeSucceededMessage(req, null, null));
        }
    })
};

exports.cancelTopActivity = function (req, res) {
    var input = req.body;

    activityService.cancelTopActivityService(input, function (err, result) {
        if (err) {
            logger.error('Exception happened when cancelTopActivity...!' + err.message);
            res.json(messageUtil.composeFailedMessage(req, 'Exception happened when cancelTopActivity...!' + err.message));
        } else {
            res.json(messageUtil.composeSucceededMessage(req, null, null));
        }
    })
};

exports.moveAllToTop = function (req, res) {
    var input = req.body;

    activityService.moveAllToTopService(input, function (err, result) {
        if (err) {
            logger.error('Exception happened when moveAllToTop...!' + err.message);
            res.json(messageUtil.composeFailedMessage(req, 'Exception happened when moveAllToTop...!' + err.message));
        } else {
            res.json(messageUtil.composeSucceededMessage(req, null, null));
        }
    })
};

exports.cancelTopAll = function (req, res) {
    var input = req.body;

    activityService.cancelTopAllService(input, function (err, result) {
        if (err) {
            logger.error('Exception happened when cancelTopAllService...!' + err.message);
            res.json(messageUtil.composeFailedMessage(req, 'Exception happened when cancelTopActivity...!' + err.message));
        } else {
            res.json(messageUtil.composeSucceededMessage(req, null, null));
        }
    })
};

exports.getAllUgc = function (req, res) {
    var page = req.params.page;
    var communityId = req.params.communityId;

    activityService.getAllUgcService(page, communityId, function (err, result) {
        if (err) {
            logger.error('Exception happened when getAllUgc...!' + err.message);
            res.json(messageUtil.composeFailedMessage(req, 'Exception happened when getAllUgc...!' + err.message));
        } else {

            var data = {
                totalPage: Math.ceil(result[1] / 20),
                allList: result[0]
            };
            res.json(messageUtil.composeSucceededMessage(req, null, data));
        }
    })
};

exports.backOfficeUpdateActivity = function (req, res) {
    var input = req.body;

    activityService.backOfficeUpdateActivityService(input, function (err, result) {
        if (err) {
            logger.error('Exception happened when backOfficeUpdateActivity...!' + err.message);
            res.json(messageUtil.composeFailedMessage(req, 'Exception happened when backOfficeUpdateActivity...!' + err.message));
        } else {
            res.json(messageUtil.composeSucceededMessage(req, null, null));

            if (input.tag != null) {
                toolUtil.putMessageQAMQ({
                    msgType: 'hobby',
                    questionId: input._id + '',
                    contentType: 'activity'
                });
            }
        }
    })
};

exports.reports = function (req, res) {
    var input = req.body;

    activityService.reports(input, function (err, result) {
        if (err) {
            logger.error('Exception happened when reports...!' + err.message);
            res.json(messageUtil.composeFailedMessage(req, 'Exception happened when reports...!' + err.message));
        } else {
            logger.info("reports: " + JSON.stringify(result));
            var data = [], itemData = {};
            for (var i = 0; i < result.length; i++) {
                itemData = {
                    nickName: result[i].reportUser.nickName ? result[i].reportUser.nickName : "",
                    desc: result[i].question ? result[i].question.desc : ""
                }
                data.push(itemData);
            }
            res.json(messageUtil.composeSucceededMessage(req, null, data));
            //logger.info("reports: "+JSON.stringify(result));
        }
    })
};

exports.changeActivityLevel = function (req, res) {
    var input = req.body;

    activityService.changeActivityLevelService(input, function (err, result) {
        if (err) {
            logger.error('Exception happened when changeActivityLevelService...!' + err.message);
            res.json(messageUtil.composeFailedMessage(req, err.message));
        } else {

            res.json(messageUtil.composeSucceededMessage(req, null, null));
        }
    })
};